﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace homework5
{
    public class OrderService
    {

        public OrderService()
        {
        }

        public List<Order> Orders
        {
            get ;set;
        }

        //按订单号排序
        public void Sort()
        {
            Orders.Sort((o1, o2) => o1.OrderId.CompareTo(o2.OrderId));
        }
        //其他排序
        public void Sort(Comparison<Order> comparison)
        {
            Orders.Sort(comparison);
        }
        //按订单号查询
        public Order SearchByNum(int n)
        {
            using(var context=new OrderContext())
            {
                var order = context.Orders.SingleOrDefault(o => o.OrderId == n);
                return order;
            }
        }
        //按客户查询
        public Order SearchByCustomer(string name)
        {
            using (var context = new OrderContext())
            {
                var order = context.Orders.SingleOrDefault(o => o.Customer.Name == name);
                return order;
            }
        }
        //按订单金额查询
        public Order SearchByMoney(double money)
        {
            using (var context = new OrderContext())
            {
                var order = context.Orders.SingleOrDefault(o => o.Money==money);
                return order;
            }
        }
        //删除订单
        public void Delete(int num)
        {
            using (var context = new OrderContext())
            {
                var order = context.Orders.Include("Details").FirstOrDefault(or => or.OrderId ==num );
                if (order != null)
                {
                    context.Orders.Remove(order);
                    context.SaveChanges();
                }
                else
                    throw new Exception("未找到订单");
            }
        }
        //修改订单顾客
        public void Amend(Order order,Customer customer)
        {
            using(var context=new OrderContext())
            {
                var orderSelect = context.Orders.FirstOrDefault(or => or.OrderId == order.OrderId);
                if (orderSelect != null)
                {
                    orderSelect.Customer = customer;
                    context.SaveChanges();
                }
            }
        }
        //修改订单序号
        public void Amend(Order order, int num)
        {
            using (var context = new OrderContext())
            {
                var orderSelect = context.Orders.FirstOrDefault(or => or.OrderId == order.OrderId);
                if (orderSelect != null)
                {
                    orderSelect.OrderId = num;
                    context.SaveChanges();
                }
            }
        }
        //修改订单细节
        public void Amend(Order order, OrderDetails od)
        {
            using (var context = new OrderContext())
            {
                var orderSelect = context.Orders.FirstOrDefault(or => or.OrderId == order.OrderId);
                if (orderSelect != null)
                {
                    orderSelect.Details = od;
                    context.SaveChanges();
                }
            }
        }
        //添加订单
        public void Add(Order order)
        {
            using(var db=new OrderContext())
            {
                db.Orders.Add(order);
                db.SaveChanges();
            }
        }
        //打印订单
        public void Print()
        {
            foreach(Order order in Orders)
            {
                Console.WriteLine(order.ToString());
            }
        }
        //订单序列化为XML文件
        public void Export(string fileName)
        {
            XmlSerializer xs = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream(fileName, FileMode.Create))
            {
                xs.Serialize(fs, Orders);
            }
        }
        //反序列化
        public void Import(string path)
        {
            XmlSerializer xs = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream(path, FileMode.Open))
            {
                using (var ctx = new OrderContext())
                {
                    List<Order> temp = (List<Order>)xs.Deserialize(fs);
                    temp.ForEach(order => {
                        if (ctx.Orders.SingleOrDefault(o => o.OrderId == order.OrderId) == null)
                        {
                            FixOrder(order);
                            ctx.Orders.Add(order);
                        }
                    });
                    ctx.SaveChanges();
                }
            }
        }

        //避免级联添加或修改Customer和Goods
        private static void FixOrder(Order newOrder)
        {
            newOrder.CustomerId = newOrder.Customer.CustomerId;
            newOrder.Customer = null;
            newOrder.Details.Good.ForEach(d => {
                d.Name = null;
                d.Num = 0;
                d.Price = 0.0;
            });
        }
    }
}
